<?php
use \GatewayWorker\Lib\Gateway;


class cmd_dealcard extends cmdbase
{
	public function onCmd($msg){

		$group_id = $this->getCurGroupId();
		

		$msg_rsp = new dealcard_rsp();
		$num = Gateway::getUidCountByGroup($group_id);
		//人数限制
		if($num < 5){
			$msg_rsp->ret = NetErrorCode::Error;
			$msg_rsp->reason = "房间人数至少5人才能玩";
			return  $msg_rsp;
		}

		$member_infos = array();
		$uid_list = Gateway::getUidListByGroup($group_id);
		
		//==========发牌 Start==========
		$count = count($uid_list);
		$pool_list = $this->createPoolList($count);
		foreach ($uid_list as $uid) {
			$member_info = new member_info();
			$member_info->uid = $uid;
			$member_info->name = ClientMgr::getNameByUid($uid);
			$member_info->support_type = SupportType::Invalid;

			//随机发一张牌
			$index = array_rand($pool_list);
			$member_info->card_type = $pool_list[$index];
			
			//发过的排，删除
			array_splice($pool_list, $index, 1);

			$member_infos[] = $member_info;
		}
		$notify_dealcard = new notify_dealcard();
		$notify_dealcard->member_infos = $member_infos;
		$notify_dealcard->uid = $this->getCurUid();
		$notify_dealcard->driver_uid = $member_infos[array_rand($member_infos)]->uid;//随意指定一个人作为火车头
		$this->sendToGroup(notify_dealcard::class, $notify_dealcard);
		//==========发牌 End==========

		//==============存起来(跨服需要) Start==============
		$group_mgr = $GLOBALS['GroupMgr'];
		$splmember_infos = $group_mgr->getSplMemberInfos($this->getCurGroupId());
		foreach ($splmember_infos as $splmember_info) {
			//离线玩家重置
			$splmember_info->support_type = SupportType::Invalid;
			$splmember_info->card_type = CardType::Invalid;
		}
		foreach ($member_infos as $member_info) {
			$splmember_info = new splmember_info();
			$splmember_info->uid = $member_info->uid;
			$splmember_info->support_type = $member_info->support_type;
			$splmember_info->card_type = $member_info->card_type;
			$splmember_infos[$splmember_info->uid] = $splmember_info;
		}
		$group_mgr->setSplMemberInfos($this->getCurGroupId(), $splmember_infos);
		//==============存起来(跨服需要) End==============

		//测试代码
		//$test = $group_mgr->getSplMemberInfos($this->getCurGroupId());
		//var_dump($test);

		//发牌成功
		$msg_rsp->ret = NetErrorCode::None;
 		return $msg_rsp;
	}

	private function createPoolList($count){
		$pool_list = array();
		switch ($count) {
			case 5:
				$pool_list[] = CardType::Merlin;
				$pool_list[] = CardType::Percival;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Morgana;
				$pool_list[] = CardType::Assassin;
				break;
			case 6:
				$pool_list[] = CardType::Merlin;
				$pool_list[] = CardType::Percival;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Morgana;
				$pool_list[] = CardType::Assassin;
				break;
			case 7:
				$pool_list[] = CardType::Merlin;
				$pool_list[] = CardType::Percival;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Morgana;
				$pool_list[] = CardType::Assassin;
				$pool_list[] = CardType::Oberon;//看不到其他坏人，其他坏人也看不到他（被孤立）。
				break;
			case 8:
				$pool_list[] = CardType::Merlin;
				$pool_list[] = CardType::Percival;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Morgana;
				$pool_list[] = CardType::Assassin;
				$pool_list[] = CardType::Mordred;//莫德雷德：梅林看不到他。
				break;
			case 9:
				$pool_list[] = CardType::Merlin;
				$pool_list[] = CardType::Percival;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Morgana;
				$pool_list[] = CardType::Assassin;
				$pool_list[] = CardType::Mordred;
				break;
			case 10:
				$pool_list[] = CardType::Merlin;
				$pool_list[] = CardType::Percival;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Pengikut;
				$pool_list[] = CardType::Morgana;
				$pool_list[] = CardType::Assassin;
				$pool_list[] = CardType::Oberon;
				$pool_list[] = CardType::Mordred;
				break;
			default:
				throw new Exception("can not handle count=$count");
				break;
		}
		return $pool_list;
	}
}